package com.gwamcc.adduser.controller;

import com.alibaba.fastjson.JSON;
import com.gwamcc.adduser.service.IindexService;
import com.gwamcc.adduser.vo.ResponseDataModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/6/23.
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    private static Logger log = LoggerFactory.getLogger(IndexController.class.getName());

    @Resource
    private IindexService indexService;

    @ResponseBody
    @RequestMapping(value = "getList", method = RequestMethod.GET)
    public Object getList(HttpServletRequest request) {
        ResponseDataModel model = new ResponseDataModel(0, "", null);
        //分页参数：每页数据量
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        //分页参数：第n页
        Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
        log.error("========== 首页获取人员机构信息 ========== 页码："+pageNum);
        //每页起始下标
        Integer startNum = (pageNum-1)*pageSize;
        //每页终止下标
        Integer endNum = pageNum*pageSize-1;

        List<Map<String, Object>> list = indexService.getList();
        log.error("========== 首页获取人员机构信息 ========== " + list);
        if(list==null || list.size()==0) {
            model.setData(list);
            model.setMessage("未获取到人员列表信息！");
            model.setStatus(1);
            return JSON.toJSON(model);
        }
        List<Map<String, Object>> relist = new ArrayList<Map<String, Object>>();
        for(int i=startNum; i<endNum-1; i++) {
            if(i > list.size()-1) break;
            relist.add(list.get(i));
        }
        if(relist.size() == 0) {
            model.setData(relist);
            model.setMessage("未获取到人员列表信息！");
            model.setStatus(1);
            return JSON.toJSON(model);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("count", relist.size());
        map.put("rows", relist);
        model.setData(map);
        model.setMessage("获取人员列表信息成功！");
        model.setStatus(0);
        return JSON.toJSON(model);
    }
}
